﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.EstRec;

namespace BAL.EstRec
{
    public class Pos
    {
        private string _idPos;
        private string _pos;
        private string _razonSocial;
        private string _codigoPostal;


        #region Properties
        public string idPos
        {
            get { return _idPos; }
            set { this._idPos = value; }
        }

        public string pos
        {
            get{ return _pos; }
            set { this._pos = value; }
        }


        public string codigoPostal
        {
            get { return _codigoPostal; }
            set { this._codigoPostal = value; }
        }

        public string razonSocial
        {
            get { return _razonSocial; }
            set { this._razonSocial = value; }
        }

        public string Canal {get;set;}
        public string SubCanal { get; set; }
        public string Potencial { get; set; }
        public string Estado { get; set; }
        public string Municipio { get; set; }
        public string Localidad { get; set; }
        public string Colonia { get; set; }

        #endregion Properties

        #region Constructors
        public Pos() { }
        public Pos(string idPos, string pos)
        {
            this.idPos = idPos;
            this.pos = pos;
        }

        public Pos(string pos)
        {
            this.pos = pos;
        }

        public Pos(string idPos, string pos, string razonsocial, string codigopostal)
        {
            this.idPos = idPos;
            this.pos = pos;
            this.razonSocial = razonsocial;
            this.codigoPostal = codigopostal;
        }
        #endregion Constructors

        public static List<Pos> GetPosList()
        {
            List<Pos> listPos;

            DataTable dt;

            string sqlStmt;

            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_v050_GET_POS;";

            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);

                listPos = dt.AsEnumerable().Select(
                            row => new Pos
                            {
                                idPos = String.IsNullOrEmpty(row.Field<string>("POS")) ? "" : row.Field<string>("POS")
                                ,
                                pos = String.IsNullOrEmpty(row.Field<string>("ID_POS")) ? "" : row.Field<string>("ID_POS")
                                ,
                                razonSocial = String.IsNullOrEmpty(row.Field<string>("RAZON_SOCIAL")) ? "" : row.Field<string>("RAZON_SOCIAL")
                                ,
                                SubCanal = String.IsNullOrEmpty(row.Field<string>("SUB_CANAL")) ? "" : row.Field<string>("SUB_CANAL")
                                ,
                                Potencial = String.IsNullOrEmpty(row.Field<string>("POTENCIAL")) ? "" : row.Field<string>("POTENCIAL")
                                ,
                                Municipio = String.IsNullOrEmpty(row.Field<string>("MUNICIPIO")) ? "" : row.Field<string>("MUNICIPIO")
                                ,
                                Localidad = String.IsNullOrEmpty(row.Field<string>("LOCALIDAD")) ? "" : row.Field<string>("LOCALIDAD")
                                ,
                                Estado = String.IsNullOrEmpty(row.Field<string>("ESTADO")) ? "" : row.Field<string>("ESTADO")
                                ,
                                Colonia = String.IsNullOrEmpty(row.Field<string>("COLONIA")) ? "" : row.Field<string>("COLONIA")
                                ,
                                Canal = String.IsNullOrEmpty(row.Field<string>("CANAL")) ? "" : row.Field<string>("CANAL")
                                ,
                                codigoPostal = String.IsNullOrEmpty(row.Field<string>("CODIGO_POSTAL")) ? "" : row.Field<string>("CODIGO_POSTAL")
                            }
                        ).ToList();
                return listPos;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return null;

        }


        public static List<Pos> GetComboPos2(string CurrentP)
        {
            List<Pos> listPos;

            DataTable dt;

            string sqlStmt;

            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_v050_GET_POS_FDV @TERRITORIOO = '"+CurrentP+"'";

            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);

                listPos = dt.AsEnumerable().Select(
                            row => new Pos
                            {
                                idPos = String.IsNullOrEmpty(row.Field<string>("POS")) ? "" : row.Field<string>("POS")
                                ,
                                pos = String.IsNullOrEmpty(row.Field<string>("ID_POS")) ? "" : row.Field<string>("ID_POS")
                            }
                        ).ToList();
                return listPos;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return null;

        }

        public static List<Pos> GetComboPos()
        {
            List<Pos> listPos;

            DataTable dt;

            string sqlStmt;

            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_v050_GET_POS";

            try
            {
                //Invertido
                dt = SrvDB.ExecuteQuery(sqlStmt);

                listPos = dt.AsEnumerable().Select(
                            row => new Pos
                            {
                                idPos = String.IsNullOrEmpty(row.Field<string>("POS")) ? "" : row.Field<string>("POS")
                                ,
                                pos = String.IsNullOrEmpty(row.Field<string>("ID_POS")) ? "" : row.Field<string>("ID_POS")
                            }
                        ).ToList();
                return listPos;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return null;
 
        }
        public static bool isPosOnCatalog(string _pos)
        {
            
            DataTable dt;

            bool res = false;

            string sqlStmt;
            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_v050_VALIDAR_POS "
                                + "@pos='@@par1';";
            sqlStmt = sqlStmt.Replace("@@par1", _pos.ToString());

            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                if (dt.Rows.Count == 1)
                {
                    if (Convert.ToBoolean(dt.Rows[0]["BANDERA"])==true)
                    {
                        res = true;    
                    }
                    else
                    {
                        res = false;    
                    }
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }

            return res;
        }       
 
        //------------------------------------
        public static bool operator ==(Pos x, Pos y)
        {
            // If reference same object including null then return true
            if (object.ReferenceEquals(x, y))
            {
                return true;
            }

            // If one object null the return false
            if (object.ReferenceEquals(x, null) || object.ReferenceEquals(y, null))
            {
                return false;
            }

            // Compare object property values
            return VersionsEqual(x, y);
        }

        public static bool operator !=(Pos x, Pos y)
        {
            return !(x == y);
        }

        public override bool Equals(object obj)
        {
            // If comparing to null return false
            if (object.ReferenceEquals(obj, null))
            {
                return false;
            }

            // Cast and compare object property values
            Pos version = obj as Pos;
            if (version != null)
            {
                return VersionsEqual(this, version);
            }
            else
            {
                return false;
            }
            
        }

        public override int GetHashCode()
        {
            int posHash = this.idPos == null ? 0 : this.idPos.GetHashCode();

            return posHash;
        }

        private static bool VersionsEqual(Pos x, Pos y)
        {
            // Compare properties for equality
            return (x.idPos == y.idPos);
        }
    }//end class
}
